package collection;

public class ConsList<E> extends AList<E>{

	E first;
	IList<E> rest;
	
	// require: first != null && rest != null
	public ConsList(E first, IList<E> rest)
	{
		// check precondition
		if (first == null || rest == null)
			throw new IllegalArgumentException("Arguments do not satisfy precondition!");
		
		this.first = first;
		this.rest = rest;
	}
	
	@Override
	public E first() 
	{
		return this.first;
	}

	@Override
	public IList<E> rest() 
	{
		return this.rest;
	}

	@Override
	public boolean isEmpty() 
	{
		return false;
	}

	@Override
	public String toString() 
	{
		if (this.rest.isEmpty())
		{
			return "[" + this.first.toString() + "]";
		}
		else
		{
			return "[" + this.first.toString() + ", " + this.rest.toString().substring(1);
		}
	}

	@Override
	public <T> T accept(IListVisitor<E, T> visitor) 
	{
		return visitor.visit(this);
	}
	
}
